/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package apicheck

import (
	param2 "did-service/bean/param"
	"did-service/common/crypto/gm"
	"did-service/plugins/apicheck/param"
	"fmt"
	"testing"
)

func TestApiCheck(t *testing.T) {
	Init()
	checker := Get()
	apiKey := "a1365585-d3fd-465b-8168-327465001870"
	apiSecret := "2c2c700121111711c5d36e6b4c2cb8eebba8ed2c350da318ef4872507402da60cee643c72bd2a49cf0a861abe111eef74e5261858fa29c3f4018cf2f05260c1f"

	//input := param2.ApplyVcWithoutDidParam{
	//	ApiKey:    apiKey,
	//	Timestamp: "123456",
	//	IssuerId:  "8f4f719f-4136-492d-a3f6-ebed7d109a2b",
	//	CptId:     "85b072f5-e7b6-40f2-adaa-8c21cda9f5f1",
	//	Claims:    "{\"ownerDid\":\"\", \"name\":\"xxx资源\", \"catelogCode\":\"\", \"industry\":\"\", \"useScen\":\"\", \"regCertNo\":\"\", \"resDesc\":\"\", \"filesHash\":\"\"}",
	//	Signature: "",
	//}
	//input := param2.ApplyDidParam{
	//	ApiKey: apiKey,
	//	Timestamp: "123456",
	//	Signature: "",
	//}
	input := param2.ApplyVpByApiKeyParam{
		ApiKey:         apiKey,
		Timestamp:      "123456",
		Signature:      "",
		Name:           "测试VP",
		VcId:           "06b272f3-bb9b-4dfe-a11c-06bb3cd7ab84",
		AdditionalInfo: "{\"a\":\"aaaaa\"}",
	}
	prvKey := "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgXcvk17ZhiGJp4Tf15PnjIhG6zNHezGFq1I3lb9rJrRegCgYIKoEcz1UBgi2hRANCAAQZrLTL/AA338QhmQWIAe/q4VzXbH6zSnk2C2hTRWfKs8fbuvlcb/oUcnwuj7uGJnbd7RLIr+ASNQEthnmOkFPY"
	pubKey := "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEGay0y/wAN9/EIZkFiAHv6uFc12x+s0p5NgtoU0VnyrPH27r5XG/6FHJ8Lo+7hiZ23e0SyK/gEjUBLYZ5jpBT2A=="
	pubKeyHex := gm.GetPubKeyHex(pubKey)
	fmt.Printf("pubKey: %s\n", pubKeyHex)
	prvKeyHex := gm.GetPrvKeyHex(prvKey)
	fmt.Printf("prvKey: %s\n", prvKeyHex)
	payload := checker.ExtractPayload(input)
	fmt.Printf("payload: %s\n", payload)
	sign := gm.SignWithBase64(prvKeyHex, []byte(payload+apiSecret))
	fmt.Printf("sign: %s\n", sign)

	input.Signature = sign
	payload = checker.ExtractPayload(input)

	acp := &param.ApiCheckParam{
		Payload:   payload,
		ApiKey:    apiKey,
		ApiSecret: apiSecret,
		Signature: sign,
		PubKey:    pubKeyHex,
	}

	result := checker.CheckSignature(acp)
	fmt.Printf("result: %t", result)
}
